<template>
  <div>
    <h1>三级分类页面</h1>
    <el-tree node-key="catId" :default-expanded-keys="defaultExpandedKeys" :expand-on-click-node="false" :data="data" :props="defaultProps">
      <span class="custom-tree-node" slot-scope="{ node, data }">
        <span>{{ node.label }}</span>
        <span>
          <el-button v-if="data.catLevel!=3" type="text" size="mini" @click="category={sort:0};append(data)">
            Append
          </el-button>
         <el-button type="text" size="mini" @click="() => update(data)">
            Update
          </el-button>
          <el-button v-if="node.childNodes.length == 0" type="text" size="mini" @click="() => remove(node, data)">
            Delete
          </el-button>
        </span>
      </span>
    </el-tree>

    <!-- Form 编辑弹窗 -->
    <el-button type="text" @click="dialogFormVisible = true"
      >打开嵌套表单的 Dialog</el-button
    >

    <el-dialog title="三级分类编辑" :visible.sync="dialogFormVisible">
      <el-form :model="category">
        <el-form-item label="分类名称" label-width="120px">
          <el-input v-model="category.name" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      defaultExpandedKeys:[],
      category: {
        sort: 0,
      },
      dialogFormVisible: false,
      data: [],
      defaultProps: {
        label: "name",
      },
    };
  },
  components: {},
  created() {
    this.getDataList();
  },
  methods: {
    //删除
    remove(node,data){
      console.log(node,data);
      let ids = [data.catId];
       this.$http({
        url: this.$http.adornUrl(`/product/category/delete`),
        method: "post",
        data: this.$http.adornData(ids, false),
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.$message({
            message: "操作成功",
            type: "success",
            duration: 1500,
          });
          this.dialogFormVisible = false;
          this.getDataList();
        } else {
          this.$message.error(data.msg);
        }
      }); 
    },
    //update修改回显
    update(data){
      this.dialogFormVisible = true;
      //回显 get post 
         this.$http({
        //路径拼接
        url: this.$http.adornUrl(`/product/category/info/${data.catId}`),
        method: "get",
        params: this.$http.adornParams(),
      }).then(({ data }) => {
        console.log(data);
        this.category = data.category;
      });
    },

    //保存
    /**
     * 使用已知的条件求未知
     * 已知条件 
     * data 被点击的对象
     * catId
     * level
     * name
     * parentCid
     * 
     *未知条件
     name
     parent_cid = data.catId
     cat_level = data.level+1
     sort:0
     * 
     */
    save() {
      let methodUrl = "save";
      if(this.category.catId){
         methodUrl = "/update";
      }
         //
      this.$http({
        url: this.$http.adornUrl(`/product/category/${methodUrl}`),
        method: "post",
        data: this.$http.adornData(this.category, false),
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.$message({
            message: "操作成功",
            type: "success",
            duration: 1500,
          });
          this.dialogFormVisible = false;
          this.getDataList();
          this.defaultExpandedKeys=[this.category.parentCid]
        } else {
          this.$message.error(data.msg);
        }
      }); 
    },
    //三级分类列表查询
    //编辑弹窗
    append(data) {
      console.log("data", data);
      this.dialogFormVisible = true;
      this.category.parentCid = data.catId;
      this.category.catLevel = data.catLevel + 1;
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      /**
       * $http
       * this代表的是创建的vue对象
       * main.js 找http
       * adornUrl('/sys/menu/list')
       * 参数怎么传递
       */
      this.$http({
        //路径拼接
        url: this.$http.adornUrl("/product/category/tree/list"),
        method: "get",
        params: this.$http.adornParams(),
      }).then(({ data }) => {
        console.log(data);
        this.data = data.page;
      });
    },
  },
};
</script>
